/**
 * 自定义字段管理
 *
 */
const middleware = require('./../middleware');
const UserDefinedFields = require('./../../models/user-defined-fields');
const PhoenixError = require('./../../models/phoenix-error');

/**
 * 创建自定义字段
 * @param req
 * @param res
 * @param next
 */
function create(req, res, next) {
  (async () => {
    const { domain, scope, name } = req.body;
    const created = await UserDefinedFields.createUserDefinedField({
      domain, scope, name,
    });
    res.json(created.toObject());
  })()
    .catch((err) => {
      next(new PhoenixError(err));
    });
}

function list(req, res, next) {
  (async () => {
    const { domain, scope } = req.query;
    const { list } = await UserDefinedFields.listUserDefinedField(domain, scope);
    res.json({ userDefinedFields: list.map((o) => o.toObject()) });
  })()
    .catch((err) => {
      next(new PhoenixError(err));
    });
}

function remove(req, res, next) {
  (async () => {
    const { key } = req.params;
    await UserDefinedFields.deleteUserDefinedField(key);
    res.status(204).json();
  })()
    .catch((err) => {
      next(new PhoenixError(err));
    });
}

function modify(req, res, next) {
  (async () => {
    const { key } = req.params;
    const { name } = req.body;
    const modified = await UserDefinedFields.modifyUserDefinedField(key, name);
    res.json(modified);
  })()
    .catch((err) => {
      next(new PhoenixError(err));
    });
}

function transposition(req, res, next) {
  (async () => {
    const { fieldIds } = req.body;
    await UserDefinedFields.transposeUserDefinedField(fieldIds);
    res.status(204).json();
  })()
    .catch((err) => {
      next(new PhoenixError(err));
    });
}

/**
 * Export model definition object.
 */
module.exports = (app, prefix) => {
  const udfsURL = `${prefix}/user-defined-fields.json`;
  const udfTranspositionURL = `${prefix}/user-defined-fields/transposition.json`;
  const udfURL = `${prefix}/user-defined-fields/:key.json`;

  app.post(udfsURL, middleware.user.requireTokenUnexpired);
  app.post(udfsURL, middleware.user.requireAvailable);
  app.post(udfsURL, middleware.user.requireAdministrator);
  app.post(udfsURL, create);

  app.get(udfsURL, middleware.user.requireTokenUnexpired);
  app.get(udfsURL, middleware.user.requireAvailable);
  app.get(udfsURL, middleware.user.requireAdministrator);
  app.get(udfsURL, list);

  app.delete(udfURL, middleware.user.requireTokenUnexpired);
  app.delete(udfURL, middleware.user.requireAvailable);
  app.delete(udfURL, middleware.user.requireAdministrator);
  app.delete(udfURL, remove);

  app.put(udfTranspositionURL, middleware.user.requireTokenUnexpired);
  app.put(udfTranspositionURL, middleware.user.requireAvailable);
  app.put(udfTranspositionURL, middleware.user.requireAdministrator);
  app.put(udfTranspositionURL, transposition);

  app.put(udfURL, middleware.user.requireTokenUnexpired);
  app.put(udfURL, middleware.user.requireAvailable);
  app.put(udfURL, middleware.user.requireAdministrator);
  app.put(udfURL, modify);
};
